<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta content="telephone=no" name="format-detection" />
    <!-- 留作将来统一添加 -->
    <TEMPLATE>${TEMPLATE}</TEMPLATE>
    <title>班级-群</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css">
    <link rel="stylesheet" type="text/css" href="../../css/index.css">
    <link rel="stylesheet" type="text/css" href="../../css/data-flex.min.css">
    <style type="text/css">
        body {
            background: rgba(255, 255, 255, 1);
        }

        .time {
            margin: 15px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(191, 191, 191, 1);
            text-align: center;
        }

        .row {
            margin: 15px;
        }

        .avatar {
            min-width: 36px;
            width: 36px;
            height: 36px;
            border-radius: 50%;
        }

        .right {
            padding-right: 5px;
            padding-bottom: 10px;
        }

        .name {
            margin-left: 5px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(101, 101, 101, 1);
        }

        .content {
            position: relative;
            display: inline-block;
            margin: 6px 0 0 16px;
            background: rgba(255, 255, 255, 1);
            box-shadow: 0px 2px 11px 0px rgba(0, 0, 0, 0.08);
            border-radius: 0px 8px 8px 8px;
            padding: 10px;
        }

        .txt {
            font-size: 14px;
            font-weight: 400;
            color: rgba(34, 34, 36, 1);
            line-height: 20px;
        }

        .angle {
            position: absolute;
            left: -9.5px;
            top: -1px;
            width: 10px;
            height: 14px;
        }

        .title {
            position: relative;
            height: 40px;
            font-size: 14px;
            font-weight: 400;
            color: rgba(25, 25, 25, 1);
            line-height: 40px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .title::after {
            content: "";
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 1px;
            background-color: rgba(212, 212, 212, 1);
            transform: scaleY(0.5);
        }

        .image {
            position: relative;
            min-height: 80px;
            margin-top: 6px;
            border-radius: 8px;
            overflow: hidden;
        }

        .image img {
            display: block;
            max-width: 100%;
            margin: 0 auto;
        }

        .image .play {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            width: 40px;
            height: 40px;
        }

        .btn {
            position: fixed;
            bottom: 0;
            width: 100%;
            height: 50px;
            background: rgba(90, 194, 255, 1);
            font-size: 16px;
            font-weight: 600;
            color: rgba(255, 255, 255, 1);
        }

        .btn-occupy {
            height: 50px;
        }

        .pt-0 {
            padding-top: 0;
        }

        .container {
            background: rgba(255, 255, 255, 1);
            box-shadow: 0px 2px 11px 0px rgba(0, 0, 0, 0.08);
            border-radius: 8px;
        }

        .head {
            margin: 0 12px;
            height: 34px;
            font-size: 12px;
            font-weight: 400;
            color: rgba(101, 101, 101, 1);
        }

        .head img {
            width: 18px;
            height: 18px;
            border-radius: 50%;
            margin-right: 5px;
        }

        .line {
            margin: 0 12px;
            height: 1px;
            background-color: #E8E8E8;
            transform: scaleY(0.5);
        }

        .body {
            padding: 0 5px 0 12px;
            overflow: hidden;
        }

        .body-txt {
            padding-right: 7px;
            font-size: 14px;
            font-weight: 400;
            color: rgba(34, 34, 36, 1);
            line-height: 20px;
            margin: 7px 0;
        }

        .body-img {
            box-sizing: border-box;
            padding-right: 7px;
            margin-bottom: 7px;
        }

        .body-img img {
            width: 100%;
            height: auto;
        }

        .length-1 {
            width: 100%;
            height: 184px;
        }

        .length-2 {
            width: 50%;
            height: 89px;
        }

        .length-3 {
            width: 33.3%;
            height: 58px;
        }
    </style>
</head>

<body>
    <div class="vueApp" v-cloak>
        <div v-for="item in listData">
            <div class="time">{{ transformDate(item.createDate) }}</div>
            <!-- <div class="row" v-if="item.type == 1" data-flex>
                <div class="avatar">
                    <img :src="item.icon" alt="">
                </div>
                <div class="right" data-flex-box="1">
                    <div class="name">
                        {{ item.nickName }}
                    </div>
                    <div class="content" @click="handleClassGroup(item)">
                        <div class="angle">
                            <img src="../../image/ui/angle.png" alt="">
                        </div>
                        <div class="txt">
                            {{ item.content }}
                        </div>
                    </div>
                </div>
            </div> -->
            <div class="row" v-if="item.type == 1">
                <div class="container" @click="handleClassGroup(item)">
                    <div class="head" data-flex="cross:center">
                        <img :src="item.icon" alt="">
                        {{ item.nickName }}
                    </div>
                    <div class="line"></div>
                    <div class="body">
                        <div class="body-txt" v-if="item.content">
                            {{ item.content }}
                        </div>
                        <div style="padding-bottom: 5px;flex-wrap: wrap;" data-flex v-if="item.url">
                            <div class="body-img" :class="getImgClassName(item.url.length)"
                                v-for="img,index in item.url">
                                <div style="border-radius: 4px;overflow: hidden;height: 100%;"><img :src="img" alt="">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-else-if="item.type == 2" data-flex>
                <div class="avatar">
                    <img :src="item.icon" alt="">
                </div>
                <div class="right" data-flex-box="1">
                    <div class="name">
                        {{ item.nickName }}
                    </div>
                    <div class="content pt-0" @click="handleClassGroup(item)">
                        <div class="angle">
                            <img src="../../image/ui/angle.png" alt="">
                        </div>
                        <div class="title">
                            {{ item.pmsName }}
                        </div>
                        <div class="image">
                            <img :src="item.pmsPic" alt="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" v-else-if="item.type == 3" data-flex>
                <div class="avatar">
                    <img :src="item.icon" alt="">
                </div>
                <div class="right" data-flex-box="1">
                    <div class="name">
                        {{ item.nickName }}
                    </div>
                    <div class="content pt-0" @click="handleClassGroup(item)">
                        <div class="angle">
                            <img src="../../image/ui/angle.png" alt="">
                        </div>
                        <div class="title">
                            {{ item.name }}
                        </div>
                        <div class="image" style="min-width: 50px;">
                            <img :src="item.pic" alt="">
                            <img class="play" src="../../image/ui/play.png" alt="">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 按钮占位 -->
        <div class="btn-occupy" v-if="userType == 2"></div>
        <div class="btn" v-if="userType == 2" data-flex="main:center cross:center" @click="toPost()">
            发布公告
        </div>
    </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/service/service-tool.js"></script>
<script type="text/javascript" src="../../script/service/service-app.js"></script>
<script type="text/javascript" src="../../script/service/service-user.js"></script>
<script type="text/javascript" src="../../script/service/service-logic.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript">
    // html 的引号一律为双引号(""),js 为单引号('')
    var USER, $S, vm, param, openPageDate;
    apiready = function () {
        $S = SERVICE();
        USER = USER();
        console.log(JSON.stringify(api.pageParam) + '=====pageParam');
        openPageDate = Date.parse(new Date());
        // 如果班级公告发生变更
        api.addEventListener({
            name: 'classNoticeChange'
        }, function (ret, err) {
            findNewMsg();
            scrollToNewMsg();
        });
        // 如果班级公告有删除
        api.addEventListener({
            name: 'classNoticeDelete'
        }, function (ret, err) {
            getPageData('headerRefreshing');
        });
        param = api.pageParam;
        fnInit();
    };
    // 初始化
    function fnInit() {
        vm = new Vue({
            el: '.vueApp',
            data: {
                userType: getUserType(),
                pageSize: 10,
                pageNo: 1,
                listData: []
            },
            methods: {
                transformDate: function (date) {
                    return formateTime(date, 'order_data');
                },
                getImgClassName: function (length) {
                    if (length === 1) {
                        return 'length-1';
                    } else if (length === 2 || length === 4) {
                        return 'length-2';
                    } else if (length === 3 || length === 5 || length === 6) {
                        return 'length-3';
                    }
                }
            }
        });
        load();
        if (USER.Get('memberType', true) == 1) {
            changeStatus();
            timer(1000 * 30);
        }
    }

    function load() {
        getPageData('headerRefreshing');
        fnAddHeaderRefreshingEvent(function () { //设置下拉刷新
            getPageData('load');
        });
    }

    function getPageData(sign) {
        if (sign == 'headerRefreshing') {
            vm.pageNo = 1;
        } else {
            vm.pageNo++;
        }
        var url = 'clsClass/findClassGroupList';
        var listDate = vm.listData[0] ? vm.listData[0].createDate : Date.parse(new Date());
        var params = {
            "classesId": param.id,
            "lastDate": listDate,
            "pageSize": vm.pageSize
        };
        var callback = function (data) {
            if (vm.pageNo == '1') {
                vm.listData = [];
            }
            var listData = data.listData;
            for (var i = 0; i < listData.length; i++) {
                var item = listData[i];
                if (item.url) {
                    item.url = item.url.split(',');
                }
            }
            api.refreshHeaderLoadDone();
            vm.listData = listData.concat(vm.listData);
            if (sign == 'headerRefreshing') {
                scrollToNewMsg();
            }
            // status 为200时,只返回data
        };
        POST(url, params, callback);
    }

    // 打开班级群即视为班级公告已读
    function changeStatus() {
        var params = {
            'classesId': param.id
        };
        var url = 'clsClass/updateStudentNoticeStatus';
        $S.POST(url, params, function (ret) {
            if (ret.status == 200) {
                api.execScript({
                    name: 'main',
                    frameName: 'tab_index_frm0',
                    script: 'getModule2();'
                })
            }
        })
    }

    function headerFn() {
        fnOpenPublicWin('班级设置', 'class/detail', {
            classesId: param.id
        });
    }

    function toPost() {
        fnOpenPublicWin('发布公告', 'class/post', {
            isBtn: true,
            btnName: '发布',
            headerFn: 'headerFn',
            classesId: param.id
        })
    }

    // 三十秒刷新一次数据
    function timer(duration) {
        setInterval(function () {
            findNewMsg();
        }, duration);
    }

    // 获取新消息
    function findNewMsg() {
        var time = vm.listData.length > 0 ? vm.listData[vm.listData.length - 1].createDate : openPageDate;
        var params = {
            'classesId': param.id,
            'currentTime': time
        };
        var url = 'clsClass/findClassGroupNewsList';
        var callback = function (data) {
            var listData = data.listData;
            for (var i = 0; i < listData.length; i++) {
                var item = listData[i];
                if (item.url) {
                    item.url = item.url.split(',');
                }
            }
            vm.listData = vm.listData.concat(listData);
        };
        POST(url, params, callback);
    }

    // 滚动条滚动到最新一条
    function scrollToNewMsg() {
        setTimeout(function () {
            api.pageDown({
                bottom: true,
                animate: false
            });
        }, 500);
    }

    function handleClassGroup(item) {
        switch (item.type) {
            case '1':
                fnOpenPublicWin(
                    '班级公告',
                    'class/notice',
                    {
                        noticeId: item.id
                    }
                );
                break;
            case '2':
                const url = "https://shop98220230.m.youzan.com/wscshop/showcase/feature?alias=QPOMQr3MUm&kdt_id=98028062";
                api.openApp({
                    iosUrl: url,
                    uri: url,
                });

                // 到商城
                // fnOpenWin({
                //     name: 'shop_mall_win',
                //     url: '../tabs/tab_shop.html',
                //     slidBackEnabled: false,
                //     reload: false,
                //     animation: {
                //         type: 'movein',
                //         subType: 'from_bottom',
                //         duration: 500
                //     },
                //     pageParam: {
                //         goodsDetail: item.businessId
                //     }
                // });
                // api.sendEvent({
                //     name: 'shop_open_detail',
                //     extra: {
                //         id: item.businessId
                //     }
                // })
                break;
            case '3':
                fnOpenPublicWin(
                    '视频详情',
                    'video/detail',
                    {
                        productsId: item.businessId,
                        isBtn: false,
                        from: 'teacherShare',
                        btnType: 'img',
                        btnName: 'ui/share',
                        headerFn: 'headerFn'
                    }
                );
                break;
        }
    }
</script>

</html>